package com.lamda.zhekou.manager;

/**
 * Created by MJiao on 15/8/16.
 */

import java.util.List;

/**
 * 封装分页信息
 */
public class PageModel<E> {

    /**
     * 分页控制标签数量
     */
    public static final int PagingBarBeginIndexTagCount = 5;

    //结果集
    private List<E> list;

    //查询记录数
    private int totalRecords;

    //每页多少条数据
    private int pageSize;

    //第几页
    private int pageNo;

    /**
     * 总页数
     *
     * @return
     */
    public int getTotalPages() {
        return (totalRecords + pageSize - 1) / pageSize;
    }

    /**
     * 包含第一个元素
     *
     * @return
     */
    public int getBeginIndex() {
        int start = (pageNo - 1) * pageSize;
        return start;
    }

    /**
     * 不包含最后一个元素
     *
     * @return
     */
    public int getEndIndex() {
        return this.getBeginIndex() + pageSize;
    }

    /**
     * 取得首页
     *
     * @return
     */
    public int getTopPageNo() {
        return 1;
    }

    public List<E> getList() {
        return list;
    }

    /**
     * 上一页
     *
     * @return
     */
    public int getPreviousPageNo() {
        if (pageNo <= 1) {
            return 1;
        }
        return pageNo - 1;
    }

    /**
     * 下一页
     *
     * @return
     */
    public int getNextPageNo() {
        if (pageNo >= getBottomPageNo()) {
            return getBottomPageNo();
        }
        return pageNo + 1;
    }

    /**
     * @return
     */
    public int getPagingBarBeginIndex() {
         int startInex = (this.pageNo / (PagingBarBeginIndexTagCount + 1)) * PagingBarBeginIndexTagCount + 1;

        return  startInex;
    }

    public int getPagingBarEndIndex() {
        return Math.min(this.getPagingBarBeginIndex() + (PagingBarBeginIndexTagCount - 1),this.getTotalPages() );
    }

    /**
     * 跳到首页是否可用
     * @return
     */
    public boolean isTopPreviousAvailable() {
        return this.pageNo / (PagingBarBeginIndexTagCount +1) == 0 ? false : true;
    }

    /**
     * 跳到上一页是否可用
     * @return
     */
    public boolean isPreviousAvailable() {
        return this.getPagingBarBeginIndex() != this.pageNo ? false : true;
    }
    /**
     * 跳到首页是否可用
     * @return
     */
    public boolean isLastAvailable() {
        return  this.getTotalPages() / (PagingBarBeginIndexTagCount +1)== this.pageNo/ (PagingBarBeginIndexTagCount +1) ? false : true;
    }

    /**
     * 跳到上一页是否可用
     * @return
     */
    public boolean isNexAvailable() {
        return this.getPagingBarEndIndex() != this.getTotalPages() ? true : false;
    }

    /**
     * 取得尾页
     *
     * @return
     */
    public int getBottomPageNo() {
        return getTotalPages();
    }

    public void setList(List<E> list) {
        this.list = list;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNo() {
        return pageNo;
    }

    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }
}